package com.jack.leetcode.每日一题;

/*** 
 * @author zhangjie
 * @date 2020/4/6 23:20
 */
public class 编辑距离 {

    public static void main(String[] args) {
        String a = "horse";
        String b = "ros";
        System.out.println(minDistance(a,b));
    }

    public static int minDistance(String word1, String word2) {
        int m = word1.length()+1;
        int n = word2.length()+1;
        int[][] dp = new int[n][m];
        for(int i =1;i<n;i++){
            dp[i][0] = dp[i-1][0]+1;
        }
        for(int j = 1;j<m;j++){
            dp[0][j] = dp[0][j-1]+1;
        }
        for(int i = 1;i<n;i++){
            for(int j = 1;j<m;j++){
                if(word2.charAt(i-1) == word1.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else{
                    int min = Math.min(dp[i-1][j-1],dp[i-1][j]);
                    min = Math.min(min,dp[i][j-1]);
                    dp[i][j] = min+1;
                }
            }
        }
        return dp[n-1][m-1];
    }
}
